﻿/*
 * 版 本 v1.0
 * Copyright (c) 2013-2018 广州市同慧信息科技有限公司
 * 创建人：ftquan
 * 日 期：2017.04.17
 * 描 述：机构管理
 */
var refreshGirdData; // 更新数据
var selectedRow;
var bootstrap = function ($, learun) {
    "use strict";
    var page = {
        init: function () {
            page.initGrid();
            page.bind();
        },
        bind: function () {
            // 搜索框初始化
            $('#txt_Keyword').on("keypress", function (e) {
                if (event.keyCode == "13") {
                    var $this = $(this);
                    var keyword = $this.val();
                    page.search({keyword: keyword});
                }
            });
            // 查询
            $('#btn_Search').on('click', function () {
                var keyword = $('#txt_Keyword').val();
                page.search({keyword: keyword});
            });
            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });
            //查看是否存在重复
            $('#lr_duplicate').on('click', function () {
                learun.postForm(config.base_server() + 'invoices/duplicateCheck?access_token=' + config.getToken().access_token, {}, function () {
                    refreshGirdData();
                });
            });
        },

        initGrid: function () {
            $('#gridtable').jfGrid({
                url: config.base_server() + 'invoices/list?access_token=' + config.getToken().access_token,
                headData: [
                    {
                        label: "流程号", name: "processCode", width: 80, align: "center",
                        formatter: function (cellvalue, row) {
                            var $div = $('<div></div>');
                            var $btn = $('<span style=\"cursor: pointer;color: darkblue;text-decoration: underline;\">' + cellvalue + '</span>');
                            $btn[0]._row = row;
                            $btn.on('click', function () {
                                var processId = row.processId;
                                var taskId = null;
                                var processName = row.processName;
                                if (learun.checkrow(processId)) {
                                    if (config.isTabs()) { //主题1
                                        learun.frameTab.open({
                                            id: processId,
                                            css: 'fa magic',
                                            name: '查看流程进度【' + processName + '】',
                                            path:  config.webportal_server + 'pages/workflow/wfmyTask/custmerWorkFlowForm.html?tabIframeId=' + processId + '&type=100' + "&processId=" + processId + "&taskId=" + taskId + '&access_token=' + config.getToken().access_token
                                        });
                                    } else {
                                        learun.layerForm({
                                            id: processId,
                                            title: '查看流程进度【' + processName + '】',
                                            url:  config.webportal_server + 'pages/workflow/wfmyTask/custmerWorkFlowForm.html?tabIframeId=' + processId + '&type=100' + "&processId=" + processId + "&taskId=" + taskId + '&access_token=' + config.getToken().access_token,
                                            height: 1080,
                                            width: 1920
                                        });
                                    }
                                }
                            });
                            $div.append($btn);
                            return $div;
                        }
                    },
                    {label: "流程名", name: "processName", width: 150, align: "left"},
                    {
                        label: "文件名", name: "name", width: 280, align: "left",
                        formatter: function (cellvalue, row) {
                            var $div = $('<div></div>');
                            var $btn = $('<span style=\"cursor: pointer;color: darkblue;text-decoration: underline;\">' + cellvalue + '</span>');
                            $btn[0]._row = row;
                            $btn.on('click', function () {
                                var $this = $(this);
                                var btnRow = $this[0]._row;
                                learun.layerForm({
                                    id: 'PreviewForm',
                                    title: '文件预览',
                                    url: config.webportal_server + 'pages/utility/previewUrl.html?url=' + btnRow.url + '&access_token=' + config.getToken().access_token,
                                    width: 1080,
                                    height: 850,
                                    btn: null
                                });
                            });
                            $div.append($btn);
                            return $div;
                        }
                    },
                    {label: "发票代码", name: "code", width: 110, align: "center"},
                    {label: "发票号码", name: "number", width: 80, align: "center"},
                    {label: "开票日期", name: "date", width: 120, align: "center"},
                    {
                        label: "是否重复", name: "isDuplicate", width: 70, align: "center",
                        formatter: function (cellvalue) {
                            if (cellvalue == 1) {
                                return "<span style='color:red;font-weight: bold;'>√</span>";
                            }
                        }
                    },
                    {label: "校验码", name: "checkCode", width: 160, align: "center"},
                    {label: "销售方名称", name: "sellerCode", width: 160, align: "left"},
                    {label: "销售方地址", name: "sellerAddress", width: 250, align: "left"},
                    {label: "纳税人识别号", name: "sellerName", width: 160, align: "left"},
                    {label: "发票金额", name: "amount", width: 110, align: "right"},
                    {label: "税额", name: "tax", width: 60, align: "right"},
                    {
                        label: "上传时间", name: "createTime", width: 130, align: "center",
                        formatter: function (value) {
                            return learun.formatDate(value, 'yyyy-MM-dd hh:mm:ss');
                        }
                    },
                    {label: "商品关键字", name: "detailJson", width: 220, align: "left"}
                ],
                dblclick: function (row) {
                    selectedRow = $('#gridtable').jfGridGet('rowdata');
                    var keyValue = row;
                    if (learun.checkrow(keyValue)) {
                        // learun.layerForm({
                        //     id: 'Form',
                        //     title: '编辑机构',
                        //     url: config.webportal_server + 'pages/system/company/form.html?access_token=' + config.getToken().access_token,
                        //     width: 750,
                        //     height: 500,
                        //     callBack: function (id) {
                        //         return top[id].acceptClick(refreshGirdData);
                        //     }
                        // });
                    }
                },
                isPage: true,
                reloadSelected: true,
                isTree: false,
                mainId: 'id'
            });
            page.search();
        },
        search: function (param) {
            $('#gridtable').jfGridSet('reload', param);
        }
    };

    // 保存数据后回调刷新
    refreshGirdData = function () {
        page.search();
    }

    page.init();
}


